//
//  MeOrderController.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/7.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "MeOrderController.h"
#import "MeOrderCell.h"
#import "MeOrderHeaderView.h"
#import "OrderDetailsViewController.h"


@implementation ProductModel

@end


@implementation OrderModel
+ (NSDictionary *)objectClassInArray{
    return @{
             @"productList" : @"ProductModel",
             };
}
@end


@interface MeOrderController ()
@property(nonatomic, strong) NSMutableArray *ordinaryOrders;
@property(nonatomic, strong) NSMutableArray *jiFenOrders;
@property(nonatomic, strong) MeOrderHeaderView *tabHeadView;
@end



@interface MeOrderController()
@property(nonatomic, strong) NSArray<OrderModel *> *orders;
@property(nonatomic, strong) NSMutableArray<OrderModel *> *nowTypeOrders;
@end

@implementation MeOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nowTypeOrders = [[NSMutableArray alloc] init];
    self.navigationItem.title = @"我的订单";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.ordinaryOrders = [[NSMutableArray alloc] initWithCapacity:10];
    [self.tableView registerNib:[UINib nibWithNibName:@"MeOrderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MeOrderCell"];
    self.tabHeadView = [[NSBundle mainBundle]loadNibNamed:@"MeOrderHeaderView" owner:nil options:nil][0];
    self.orders = [[NSArray alloc] init];
    
    self.tableView.estimatedRowHeight = 250;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self loadOrder];
}

-(void) loadOrder {
    NSDictionary *param = @{@"userId": AppManager.userId, @"offset": @"0", @"limit": @"1000", @"sort": @"", @"order": @""};
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/getOrderList" parameters:param method:@"GET" callBack:^(id responseObject) {
        if ([responseObject[@"state"] integerValue]) {
            self.orders = [OrderModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            for (OrderModel *model in self.orders) {
                if ([model.orderType isEqualToString:@"1"] || [model.orderType isEqualToString:@"2"]) {
                    
                }else {
                    [self.nowTypeOrders addObject:model];
                }
                
            }
            [self.tableView reloadData];
            return;
        }
        [MozTopAlertView showWithType:1 text:@"获取订单失败." parentView:nil];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.nowTypeOrders.count;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     MeOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeOrderCell" forIndexPath:indexPath];
     cell.orderModel = self.nowTypeOrders[indexPath.row];
     cell.selectionStyle = UITableViewCellSeparatorStyleNone;
     return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailsViewController *orderDetailViewCon = [[OrderDetailsViewController alloc] init];
    orderDetailViewCon.orderModel = self.nowTypeOrders[indexPath.row];
    [self.navigationController pushViewController:orderDetailViewCon animated:true];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderModel *orderM = self.nowTypeOrders[indexPath.row];
    return 145 + orderM.productList.count * 91 + UITableViewAutomaticDimension;  // 140
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.tabHeadView) {
        return self.tabHeadView;
    }
    return self.tabHeadView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
